package com.gdou.service.impl;

import com.gdou.commons.Result;
import com.gdou.mapper.DeptMapper;
import com.gdou.mapper.UserMapper;
import com.gdou.dto.UserDto;
import com.gdou.dto.UserSearchDto;
import com.gdou.model.User;
import com.gdou.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 分页查询员工信息
     *
     * @param pageNum  页码
     * @param pageSize 每页行数
     * @return 同于返回类型
     */
    @Override
    public Result findUsersByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userMapper.selectUsersByPage();//查所有
        PageInfo<User> pageInfo = new PageInfo<>(userList);//分页处理
        Result result = new Result(200,"访问成功");
        result.setData(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    // 添加职员
    @Override
    public Result saveUser(UserDto userDto) {
        /*判断部门是否存在*/
        int rows = deptMapper.selectDeptExists(userDto.getDeptId());
        if (rows<1){
            /*部门不存在*/
            return new Result(-1,"部门不存在");
        }

        rows = userMapper.selectUserExists(userDto);
        if (rows>0){
            /*职员已存在*/
            return new Result(-1,"职员已存在");
        }

        /*添加职员信息*/
        rows = userMapper.insertUser(userDto);
        if (rows<1){
            /*部门不存在*/
            return new Result(-1,"添加失败");
        }

        return Result.SUCCESS;
    }

    @Override
    public Result removeOneUser(Long id) {
        if (id<1){
            return Result.DATA_FORMAT_ERROR;
        }
        int rows =  userMapper.deleteById(id);
        if (rows<1){
            return new Result(-1,"删除失败");
        }
        return new Result();
    }

    @Override
    public Result removeManyUser(String id) {
        if (id==null||!(id+",").matches("([1-9][0-9]*,)+")){
            return Result.DATA_FORMAT_ERROR;
        }
        int rows = userMapper.deleteInIds(id);
        if (rows<1){
            return new Result(-1,"删除失败");
        }
        return new Result();
    }

    @Override
    public Result modifyUser(UserDto userDto) {
        int rows = deptMapper.selectDeptExists(userDto.getDeptId());
        if (rows<1){
            return new Result(-1,"部门不存在");
        }

        rows = userMapper.selectUserById(userDto.getId());
        if (rows<1){
            return new Result(-1,"员工不存在");
        }

        rows = userMapper.updateUser(userDto);
        if (rows<1){
            return new Result(-1,"修改失败");
        }

        return new Result();
    }

    @Override
    public Result findUsersBySearch(UserSearchDto userSearchDto) {
        PageHelper.startPage(userSearchDto.getPageNum(), userSearchDto.getPageSize());
        List<User> userList = userMapper.selectUsersBySearch(userSearchDto);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        Result result = new Result();
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
}
